package com.qc31.comlib.choose.car

import android.app.Activity
import android.view.View
import android.widget.ImageView
import androidx.recyclerview.widget.LinearLayoutManager
import autodispose2.autoDispose
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.viewholder.BaseViewHolder
import com.qc31.baselib.base.LazyFragment
import com.qc31.baselib.databinding.IncludeSelectSureBinding
import com.qc31.baselib.rx.clickThrottle
import com.qc31.baselib.utils.*
import com.qc31.comlib.Keys
import com.qc31.comlib.R
import com.qc31.comlib.databinding.FragmentHistoryCarBinding
import com.qc31.comlib.entity.HistoryCars

/**
 *@Author Jasper
 *@Time   2020/7/22
 *@Desc   历史车辆
 */
class HistoryFragment(private val isVideo: Boolean = false) :
    LazyFragment<FragmentHistoryCarBinding>(FragmentHistoryCarBinding::inflate) {
    private lateinit var mSureVB: IncludeSelectSureBinding
    private var carInfo: HistoryCars.ListBean? = null

    //空布局
    private val emptyView by lazy {
        val view=    layoutInflater.inflate(
            R.layout.layout_empty_view,
            binding.includeRecycler.recyclerView,
            false
        )
        view.findViewById<ImageView>(R.id.ivNoData).loadGif(requireContext(), R.mipmap.ic_empty)
        view
    }



    override fun initView(view: View) {
        mSureVB = IncludeSelectSureBinding.bind(binding.root)

        ChooseCars.isVideo = isVideo
        mSureVB.tvSure.setText(R.string.desc_com_sure)
    }

    override fun initData() {

        ChooseCars.initData()
        binding.includeRecycler.recyclerView.layoutManager = LinearLayoutManager(context)
        binding.includeRecycler.recyclerView.adapter = HistoryCarAdapter().apply {
            setOnItemClickListener { adapter, view, position ->
                hideSoftInput()
                index = position
                notifyDataSetChanged()
                carInfo = data[position]
            }
            ChooseCars.listObservable.hide().autoDispose(scopeProvider).subscribe {
                if (it.isEmpty()) {
                    setEmptyView(emptyView)
                    mSureVB.tvSure.visibility = View.GONE
                } else {
                    setList(it)
                    mSureVB.tvSure.visibility = View.VISIBLE
                }
            }
        }
    }

    override fun initEvent() {
        mSureVB.tvSure.clickThrottle().autoDispose(scopeProvider).subscribe {
            if (carInfo == null) {
                context?.toast(R.string.hint_toast_choose_car)
                return@subscribe
            } else {
                carInfo?.run {
                    ChooseCars.addCars(
                        carId = carId,
                        carPlate = carPlate,
                        carName = carName,
                        videos = videos,
                        iconLink = iconLink,
                        isVideo = !videos.isNullOrEmpty()
                    )
                    setResultTo(activity as Activity) {
                        putExtra(Keys.INTENT_CAR_ID, carId)
                        putExtra(Keys.INTENT_CAR_NAME, subPlate(carPlate, 14))
                        if (videos != null) putExtra(Keys.VIDEO, videos)
                    }
                }
                activity?.finish()
            }
        }
    }

    inner class HistoryCarAdapter :
        BaseQuickAdapter<HistoryCars.ListBean, BaseViewHolder>(R.layout.item_search_choose_car) {
        var index: Int = -1
        override fun convert(holder: BaseViewHolder, item: HistoryCars.ListBean) {
            item.run {

                val check = holder.getView<ImageView>(R.id.ivItemChoose)
                check.isSelected = index == holder.layoutPosition
                holder.getView<ImageView>(R.id.ivCarLogo).loadStringDImgError(context, iconLink)
                holder.setText(R.id.tvCarPlate, carPlate)
                holder.setText(R.id.tvCarName, carName)

            }
        }
    }
}